package cn.apienvrypt.util;

import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

/**
 * 参数拦截器共用方法
 *
 * @author admin
 */
public class ParamHandleUtil {

	public static Object result(JsonElement param, boolean require, String parameterName, Class<?> parameterType,
		Class<?> typeInList) throws Exception {
		Gson gson = GsonUtil.getGson();
		try {
			if(param == null || param.isJsonNull()) {
				if(require) {
					throw new RuntimeException("参数不能为空：" + parameterName + "，其类型为" + parameterType.getSimpleName());
				} else {
					return null;
				}
			}
			if(parameterType == Date.class) {
				return toDate(param.getAsString(), parameterName);
			}
			if(parameterType == String.class) {
				if(param.isJsonArray()) {
					return param.getAsJsonArray()
						.toString();
				} else if(param.isJsonObject()) {
					return param.getAsJsonObject()
						.toString();
				} else {
					return param.getAsString();
				}
			}
			if(parameterType == List.class) {
				// 只适用原始包装类的List，如需支持复杂对象List的需要
				String paramString;
				if(!param.isJsonArray()) {
					paramString = param.getAsString();
					if("".equals(paramString) || "[]".equals(paramString)) {
						return null;
					}
				} else {
					paramString = param.toString();
				}
				if(typeInList == Boolean.class) {
					return GsonUtil.getGson()
						.fromJson(paramString, new TypeToken<List<Boolean>>() {
						}.getType());
				} else if(typeInList == Integer.class) {
					return gson.fromJson(paramString, new TypeToken<List<Integer>>() {
					}.getType());
				} else if(typeInList == Double.class) {
					return gson.fromJson(paramString, new TypeToken<List<Double>>() {
					}.getType());
				} else if(typeInList == Long.class) {
					return gson.fromJson(paramString, new TypeToken<List<Long>>() {
					}.getType());
				} else if(typeInList == Float.class) {
					return gson.fromJson(paramString, new TypeToken<List<Float>>() {
					}.getType());
				} else {
					return gson.fromJson(paramString, new TypeToken<List<String>>() {
					}.getType());
				}
			}
			if(parameterType == Integer.class) {
				try {
					return param.getAsInt();
				} catch(Exception e) {
					return param.getAsBoolean() ? 1 : 0;
				}
			}
			return gson.fromJson(param.getAsString(), parameterType);
		} catch(Exception e) {
			throw e;
		}
	}

	public static JsonObject toJsonObject(JsonElement jsonElement) {
		if(jsonElement.isJsonObject()) {
			return jsonElement.getAsJsonObject();
		}
		String str = jsonElement.getAsString();
		if(str != null && !"".equals(str)) {
			return JsonParser.parseString(str)
				.getAsJsonObject();
		}
		return new JsonObject();
	}

	public static Date toDate(String dateString, String parameterName) throws Exception {
		try {
			return DateConvertConfig.convertDate(dateString);
		} catch(Exception e) {
			throw new RuntimeException("参数错误，无效的时间格式：" + parameterName + "=" + dateString);
		}
	}
}
